package DBLayer;
import java.sql.*;
/**
 * @Author Ricki Jensen
 * @Version 2011.25.01
 */

public class DbConnection1
{   //constants used to get access to the database
 //SQL Server
 private static final String  driver = "jdbc:sqlserver://balder.ucn.dk:1433";
 //private static final String  driver = "jdbc:sqlserver://balder.ucn.dk:1433";
    private static final String  databaseName = ";database=DM77_1";
 //private static final String  databaseName = ";database=RIJ";
    //SQL Server
    private static String userName = ";user=DM77_1";
    private static String password = ";password=MaaGodt";
 //private static String userName = ";user=rij";
 //private static String password = ";password=2345678";
 
    private DatabaseMetaData dma;
    private static Connection con;
    // an instance of the class is generated
    private static DbConnection1  instance = null;


    // the constructor is private to ensure that only one object of this class is created
    private DbConnection1()
    {
     String url = driver + databaseName + userName + password;

        try{
            //load af driver
            //SQL Server
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            System.out.println("Load af class ok");
          
        }
        catch(Exception e){
            System.out.println("Can not find the driver");
            System.out.println(e.getMessage());
        }//end catch
        try{
            //connection to the database
           
            con = DriverManager.getConnection(url);
            //set autocommit
            con.setAutoCommit(true);
            dma = con.getMetaData(); // get meta data
            System.out.println("Connection to " + dma.getURL());
            System.out.println("Driver " + dma.getDriverName());
            System.out.println("Database product name " + dma.getDatabaseProductName());
        }//end try
        catch(Exception e){

            System.out.println("Problems with the connection to the database");
            System.out.println(e.getMessage());
            System.out.println(url);
        }//end catch
    }//end  constructor
    
  //closeDb: closes the connection to the database
    public static void closeConnection()
    {
        try{
            con.close();
            System.out.println("The connection is closed");
        }
         catch (Exception e){
            System.out.println("Error trying to close the database " +  e.getMessage());
         }
    }//end closeDB
  
    //getDBcon: Get-metode, returnerer forbindelsen til databasen
    public  Connection getDBcon()
    {
       return con;
    }
    //this method is used to get the instance of the connection
    public static DbConnection1 getInstance()
    {
        if (instance == null)
        {
          instance = new DbConnection1();
        }
        return instance;
    }
    public static void startTransaction()
    { try{
        con.setAutoCommit(false);
        }
      catch(Exception e){
        System.out.println("error start transaction");
        System.out.println(e.getMessage());
      }
    }
    public static boolean commitTransaction()
    { try{
 con.commit();
        con.setAutoCommit(true);
        return true;
        }
      catch(SQLException e){
        System.out.println("error commit transaction");
        System.out.println(e.getMessage());
        return false;
      }
    }      
    public static void rollbackTransaction()
    { try{
        con.rollback();
        con.setAutoCommit(true);
        }
      catch(Exception e){
        System.out.println("error rollback transaction");
        System.out.println(e.getMessage());
      }
    }
}//end DbConnection